import 'package:drive/src/utils/ddialog/view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DTextField extends StatefulWidget {
  final String? placeholder;
  final Color placeholderColor;
  final Color fontColor;
  final bool isDeleteButton;
  final double fontSize;
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final int? maxLength;
  final EdgeInsetsGeometry padding;
  final bool obscureText;
  final double buttonSize;

  final bool enable;
  final bool readOnly;
  final TextAlignVertical? textAlignVertical;
  final TextInputAction? textInputAction;

  final Function(String)? onSubmitted;

  final Function(String)? onChanged;
  final Function? onDeleteClick;

  const DTextField(
      {this.obscureText = false,
      this.textInputAction,
        this.onDeleteClick,
      this.focusNode,
        this.textAlignVertical,
      this.isDeleteButton = true,
      this.fontSize = 12,
        this.enable=true,
        this.buttonSize=12,
        this.readOnly=false,
      this.placeholder,
      this.fontColor = const Color(0xff262627),
      this.placeholderColor = const Color(0xffbebebf),
      Key? key,
      this.controller,
      this.maxLength,
      this.onSubmitted,
      this.onChanged,
      this.padding = const EdgeInsets.all(6)})
      : super(key: key);

  @override
  State<DTextField> createState() => _DTextFieldState();
}

class _DTextFieldState extends State<DTextField> {
  late TextEditingController controller;
  late FocusNode focusNode;
  bool showDeleteButton = false;

  @override
  void initState() {
    super.initState();
    controller = widget.controller ?? TextEditingController();
    focusNode = widget.focusNode ?? FocusNode();
    if (widget.isDeleteButton) initListener();
  }

  void initListener() {
    controller.addListener(() {
      final show = focusNode.hasFocus && controller.text.isNotEmpty;
      if (show == showDeleteButton) return;
      showDeleteButton = show;
      setState(() {});
    });
    focusNode.addListener(() {
      final show = focusNode.hasFocus && controller.text.isNotEmpty;
      if (show == showDeleteButton) return;
      showDeleteButton = show;
      setState(() {});
    });
  }



  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
            child: CupertinoTextField(
          decoration: BoxDecoration(),
          obscureText: widget.obscureText,
          textInputAction: widget.textInputAction,
          maxLength: widget.maxLength,
          placeholder: widget.placeholder,
          controller: controller,
          focusNode: focusNode,
          readOnly: widget.readOnly,
          onChanged: widget.onChanged,
          enabled: widget.enable,
          padding: widget.padding,
              textAlignVertical:widget.textAlignVertical ,
          onSubmitted: widget.onSubmitted,
          style: TextStyle(
              inherit: false,
              fontSize: widget.fontSize,
              color: widget.fontColor,
              height: 1.4),
          placeholderStyle: TextStyle(
              inherit: false,
              fontSize: widget.fontSize,
              color: widget.placeholderColor,
              height: 1.4),
        )),
        if(showDeleteButton&&!widget.readOnly)
        GestureDetector(
          onTap: onDelClick,
          behavior: HitTestBehavior.opaque,
          child: Container(
            padding: EdgeInsets.only(left: 5,right: 2),
            alignment: Alignment.centerLeft,
            child: Container(
                decoration: BoxDecoration(color: Color(0xff8C8C8D),borderRadius: BorderRadius.circular(widget.buttonSize/2)),
                width: widget.buttonSize,
                height: widget.buttonSize,
                alignment: Alignment.center,
                child: Icon(
                  Icons.close,
                  size: (widget.buttonSize*0.8).toInt().toDouble(),
                  color: Color(0xfff0f0f1),
                )),
          ),
        )
      ],
    );
  }

  void onDelClick() {
    controller.clear();
      widget.onDeleteClick?.call();
  }
}
